﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SisVenda.Web.Restrito.Fn;
using System.Data;
using SisVenda.Web.messageBox;

namespace SisVenda.Web.Restrito
{
    public partial class CadastroFuncionario : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnPesquisar_Click(object sender, EventArgs e)
        {
            gvwFuncionario.DataBind();
        }

        protected void ddlConsulta_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void gvwFuncionario_DataBinding(object sender, EventArgs e)
        {
            gvwFuncionario.PageSize = Convert.ToInt16(ddlConsulta.SelectedValue);
        }

        protected void ddlUF_SelectedIndexChanged(object sender, EventArgs e)
        {
            ((ObjectDataSource)fvwEdicao.FindControl("odsCidade")).DataBind();

        }

        protected void gvwFuncionario_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (fvwEdicao.CurrentMode != FormViewMode.ReadOnly)
                fvwEdicao.ChangeMode(FormViewMode.ReadOnly);
            odsFormView.DataBind();
            fvwEdicao.DataBind();

        }

        protected void odsFormView_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            if (gvwFuncionario.SelectedDataKey != null)
            {
                e.InputParameters["idFuncionario"] = gvwFuncionario.SelectedDataKey.Value;
            }
        }

        protected void ddlCargo_SelectedIndexChanged(object sender, EventArgs e)
        {
            CadastroFucionarioFn cf = new CadastroFucionarioFn();
            DataTable dt = new DataTable();
            dt = cf.ObterCargoid(Convert.ToInt32(((DropDownList)fvwEdicao.FindControl("ddlCargo")).SelectedValue.ToString()));
            ((TextBox)fvwEdicao.FindControl("tbxCargHoraria")).Text = dt.Rows[0]["cargaHoraria"].ToString();
            ((TextBox)fvwEdicao.FindControl("tbxCargHoraria")).Enabled = false;
            ((TextBox)fvwEdicao.FindControl("tbxSalarioBase")).Text = dt.Rows[0]["salarioBase"].ToString();
            ((TextBox)fvwEdicao.FindControl("tbxSalarioBase")).Enabled = false;

            cf = null;
            dt = null;
        }

        protected void odsFormView_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
        {

            e.InputParameters["nome"] = ((TextBox)fvwEdicao.FindControl("tbxNome")).Text;
            e.InputParameters["cpf"] = ((TextBox)fvwEdicao.FindControl("tbxCPF")).Text;
            e.InputParameters["rg"] = ((TextBox)fvwEdicao.FindControl("tbxRG")).Text;
            e.InputParameters["cnpj"] = "";
            e.InputParameters["inscricaoEstadual"] = "";
            e.InputParameters["sexo"] = ((DropDownList)fvwEdicao.FindControl("ddlSexo")).SelectedValue;
            e.InputParameters["tipoPessoa"] = "F";
            e.InputParameters["dataNascimento"] = Convert.ToDateTime(((TextBox)fvwEdicao.FindControl("tbxDataNasci")).Text);
            e.InputParameters["estadoCivil"] = ((DropDownList)fvwEdicao.FindControl("ddlEstadoCivil")).SelectedValue;
            e.InputParameters["telefone"] = ((TextBox)fvwEdicao.FindControl("tbxTelefone")).Text;
            e.InputParameters["celular"] = ((TextBox)fvwEdicao.FindControl("tbxCeluar")).Text;
            e.InputParameters["fax"] = ((TextBox)fvwEdicao.FindControl("tbxFax")).Text;
            e.InputParameters["email"] = ((TextBox)fvwEdicao.FindControl("tbxEmail")).Text;
            e.InputParameters["descricao"] = ((TextBox)fvwEdicao.FindControl("tbxEndereco")).Text;
            e.InputParameters["bairro"] = ((TextBox)fvwEdicao.FindControl("tbxBairro")).Text;
            e.InputParameters["numero"] = ((TextBox)fvwEdicao.FindControl("tbxNumero")).Text;
            e.InputParameters["cep"] = ((TextBox)fvwEdicao.FindControl("tbxCep")).Text;
            e.InputParameters["idCidade"] = Convert.ToInt32(((DropDownList)fvwEdicao.FindControl("ddlCidade")).SelectedValue);
            e.InputParameters["idCargo"] = Convert.ToInt32(((DropDownList)fvwEdicao.FindControl("ddlCargo")).SelectedValue);

            odsFuncionario.DataBind();
            MessageBox.Show("Ação Efetuada com Sucesso!", MessageBox.MessageType.Success);

        }

        protected void ddlACargo_SelectedIndexChanged(object sender, EventArgs e)
        {
            CadastroFucionarioFn cf = new CadastroFucionarioFn();
            DataTable dt = new DataTable();
            dt = cf.ObterCargoid(Convert.ToInt32(((DropDownList)fvwEdicao.FindControl("ddlACargo")).SelectedValue.ToString()));
            ((TextBox)fvwEdicao.FindControl("tbxACargHoraria")).Text = dt.Rows[0]["cargaHoraria"].ToString();
            ((TextBox)fvwEdicao.FindControl("tbxACargHoraria")).Enabled = false;
            ((TextBox)fvwEdicao.FindControl("tbxASalarioBase")).Text = dt.Rows[0]["salarioBase"].ToString();
            ((TextBox)fvwEdicao.FindControl("tbxASalarioBase")).Enabled = false;

            cf = null;
            dt = null;
        }

        protected void ddlUFA_SelectedIndexChanged(object sender, EventArgs e)
        {
            ((ObjectDataSource)fvwEdicao.FindControl("odscidadeup")).DataBind();
        }

        protected void odsFuncionario_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            if (tbxCle_Nome.Text != "")
                e.InputParameters["nome"] = tbxCle_Nome.Text;
            if (tbxFunc_Cpf.Text != "")
                e.InputParameters["cpf"] = tbxFunc_Cpf.Text;
        }

        protected void odsFormView_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            gvwFuncionario.DataBind();
        }

        protected void odsFormView_Updated(object sender, ObjectDataSourceStatusEventArgs e)
        {
            gvwFuncionario.DataBind();
        }

        protected void odsFormView_Updating(object sender, ObjectDataSourceMethodEventArgs e)
        {
            /*
             * <asp:Parameter Name="func_id" Type="Int32" />
                    <asp:Parameter Name="end_id" Type="Int32" />
                    <asp:Parameter Name="pes_nome" Type="String" />
                    <asp:Parameter Name="pes_cpf" Type="String" />
                    <asp:Parameter Name="pes_rg" Type="String" />
                    <asp:Parameter Name="pes_cnpj" Type="String" />
                    <asp:Parameter Name="pes_inscricaoEstadual" Type="String" />
                    <asp:Parameter Name="pes_sexo" Type="String" />
                    <asp:Parameter Name="pes_tipoPessoa" Type="String" />
                    <asp:Parameter Name="pes_dataNascimento" Type="DateTime" />
                    <asp:Parameter Name="pes_estadoCivil" Type="String" />
                    <asp:Parameter Name="pes_telefone" Type="String" />
                    <asp:Parameter Name="pes_celular" Type="String" />
                    <asp:Parameter Name="pes_fax" Type="String" />
                    <asp:Parameter Name="pes_email" Type="String" />
                    <asp:Parameter Name="end_descricao" Type="String" />
                    <asp:Parameter Name="end_bairro" Type="String" />
                    <asp:Parameter Name="end_numero" Type="Int32" />
                    <asp:Parameter Name="end_cep" Type="String" />
                    <asp:Parameter Name="cid_id" Type="Int32" />
                    <asp:Parameter Name="car_id" Type="Int32" />
             * */
            e.InputParameters["idFuncionario"] = Convert.ToInt32(((Label)fvwEdicao.FindControl("lblidup")).Text);
            e.InputParameters["idEndereco"] = Convert.ToInt32(((Label)fvwEdicao.FindControl("lblendidup")).Text);
            e.InputParameters["nome"] = ((TextBox)fvwEdicao.FindControl("tbxANome")).Text;
            e.InputParameters["cpf"] = ((TextBox)fvwEdicao.FindControl("tbxACPF")).Text;
            e.InputParameters["rg"] = ((TextBox)fvwEdicao.FindControl("tbxARG")).Text;
            e.InputParameters["cnpj"] = "";
            e.InputParameters["inscricaoEstadual"] = "";
            e.InputParameters["sexo"] = ((DropDownList)fvwEdicao.FindControl("ddlASexo")).SelectedValue;
            e.InputParameters["tipoPessoa"] = "F";
            e.InputParameters["dataNascimento"] = Convert.ToDateTime(((TextBox)fvwEdicao.FindControl("tbxADataNasci")).Text);
            e.InputParameters["estadoCivil"] = ((DropDownList)fvwEdicao.FindControl("ddlAEstadoCivil")).SelectedValue;
            e.InputParameters["telefone"] = ((TextBox)fvwEdicao.FindControl("tbxATelefone")).Text;
            e.InputParameters["celular"] = ((TextBox)fvwEdicao.FindControl("tbxACeluar")).Text;
            e.InputParameters["fax"] = ((TextBox)fvwEdicao.FindControl("tbxAFax")).Text;
            e.InputParameters["email"] = ((TextBox)fvwEdicao.FindControl("tbxAEmail")).Text;
            e.InputParameters["descricao"] = ((TextBox)fvwEdicao.FindControl("tbxAEndereco")).Text;
            e.InputParameters["bairro"] = ((TextBox)fvwEdicao.FindControl("tbxABairro")).Text;
            e.InputParameters["numero"] = ((TextBox)fvwEdicao.FindControl("tbxANumero")).Text;
            e.InputParameters["cep"] = ((TextBox)fvwEdicao.FindControl("tbxACep")).Text;
            e.InputParameters["idCidade"] = Convert.ToInt32(((DropDownList)fvwEdicao.FindControl("ddlACidade")).SelectedValue);
            e.InputParameters["idCargo"] = Convert.ToInt32(((DropDownList)fvwEdicao.FindControl("ddlACargo")).SelectedValue);
        }
    }
}